import IEntitySystem from "../../base/ECSR/IEntitySystem";
import Entity from "../../../scripts/base/ECSR/Entity";
import {EntityLifeState} from "../../../scripts/base/ECSR/Entity";
import TimeUtil  from "../../../common/utils/TimeUtil";
import NotifyMgr, { SelfNotification } from "../../../scripts/base/notify/NotifyMgr";
import Notifications from "../Notifications";

export default class RemoveEntitySystem extends IEntitySystem
{
    constructor(world)
    {
        super(world);
    }
    // 每逻辑帧调用
    Execute()
    {
        let t1 = TimeUtil.GetCurrTimeMs();
        let entities = this.world.GetEntities();
        let removeEntities = [];
        for(let i = 0; i < entities.length; ++i)
        {
            if(entities[i].lifeState == EntityLifeState.Die)
            {
                removeEntities.push(entities[i].id);
            }
        }
        let t2 = TimeUtil.GetCurrTimeMs();
        // cc.log("removeEntites length:" + removeEntities.length);
        for(let i = 0; i < removeEntities.length; ++i)
        {
            this.world.RemoveEntity(removeEntities[i]);
            NotifyMgr.Instance().Send(Notifications.RemoveEntity, new SelfNotification("", [removeEntities[i]], this))
        }
        let t3 = TimeUtil.GetCurrTimeMs();
        // cc.log("RemoveEntitySystem|cost1=" + (t2 - t1) + ", cost2=" + (t3 - t2));
    }
}